﻿using System.Collections.Generic;
using BitBenderGames;
using LogSystem;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    public class CityLODManager : MonoBehaviour
    {
        private MobileTouchCamera _mobileTouchCamera;
        private TouchCameraController _touchCameraController;
        private CityEventManager _cityEventManager;
        
        public static float Default_Start_Height = 0;
        [SerializeField]
        private List<float> m_CameraHeightList;
        [SerializeField]
        private int m_CameraHeightCnt;
        [SerializeField]
        private float m_MaxFov;
        [SerializeField]
        private float m_MinFov;
        [SerializeField]
        private int m_LastLODLevel;
        [SerializeField]
        private float m_LastLODPercent;
        

        public void Init(MobileTouchCamera mobileTouchCamera,
            TouchCameraController touchCameraController,
            CityEventManager cityEventManager)
        {
            if (mobileTouchCamera == null)
            {
                return;
            }
            
            _mobileTouchCamera = mobileTouchCamera;
            _touchCameraController = touchCameraController;
            _cityEventManager = cityEventManager;

            m_CameraHeightList = new List<float>()
            {
                _touchCameraController.LOD1,
                _touchCameraController.LOD2,
                _touchCameraController.LOD3,
                _touchCameraController.LOD4,
                _touchCameraController.LOD5
            };
            m_CameraHeightCnt = m_CameraHeightList.Count;
            m_MinFov = _touchCameraController.MIN_FOV;
            m_MaxFov = _touchCameraController.MAX_FOV;
        }

        public void OnUpdate()
        {
            if (_touchCameraController == null) return;
            
            float currentheight = GetCameraHeight() + 0.001f;
            float currentFOV = GetCameraFOV();
            
            bool result = true;

            int prevLodLevel = m_LastLODLevel;
            float prevLodPercent = m_LastLODPercent;
            
            if (_touchCameraController.ZoomMode == PerspectiveZoomMode.FIELD_OF_VIEW)
            {
                m_LastLODLevel = 0;
                m_LastLODPercent = (currentFOV - m_MinFov) * 100.0f/ (m_MaxFov - m_MinFov);
                m_LastLODPercent = Mathf.Max(0, Mathf.Min(m_LastLODPercent, 100));
                result = false;
            }
            else
            {
                if (currentheight >= m_CameraHeightList[m_CameraHeightCnt - 1])
                {
                    m_LastLODLevel = m_CameraHeightCnt - 1;
                    m_LastLODPercent = 100;
                    result = true;
                }
                else if (currentheight <= m_CameraHeightList[0])
                {                    
                    m_LastLODLevel = 0;
                    m_LastLODPercent = 0;
                    result = true;
                }
                else
                {
                    for (int i = 0; i < m_CameraHeightCnt; i++)
                    {
                        var prevHeight = m_CameraHeightList[i];
                        var nextHeight = m_CameraHeightList[i + 1];

                        if (prevHeight <= currentheight && currentheight < nextHeight)
                        {
                            if (i != 0)
                            {
                                // 找到了符合区间范围的LOD等级
                                m_LastLODLevel = i;
                                m_LastLODPercent = ((currentheight - prevHeight) * 100.0f / (nextHeight - prevHeight));
                                result = false;
                                break;
                            }
                            else
                            {
                                m_LastLODLevel = 0;
                                m_LastLODPercent = (currentFOV - m_MinFov) * 100.0f / (m_MaxFov - m_MinFov);
                                m_LastLODPercent = Mathf.Max(0, Mathf.Min(m_LastLODPercent, 100));
                                result = false;
                                break;
                            }
                        }
                    }
                }
            }

            if (result)
            {
                // 如果没有找到区间。dis小于起始高度则是最小等级，否则为最大等级
                if (currentheight < Default_Start_Height)
                {
                    m_LastLODLevel = 0;
                    m_LastLODPercent = 0;
                }
                else
                {
                    m_LastLODLevel = m_CameraHeightCnt - 1;
                    m_LastLODPercent = 100;
                }
            }


            if (prevLodLevel != m_LastLODLevel)
            {
                _cityEventManager.OnLodLevelChanged(m_LastLODLevel,prevLodLevel);
            }

            if (Mathf.Abs(prevLodPercent - m_LastLODPercent) > 0.005f)
            {
                _cityEventManager.OnLodPercentChanged(m_LastLODLevel,currentheight,m_LastLODPercent,prevLodPercent);
            }
            
        }


        private float GetCameraHeight()
        {
            return _mobileTouchCamera.Cam.transform.position.y;
        }

        private float GetCameraFOV()
        {
            return _mobileTouchCamera.Cam.fieldOfView;
        }
        
        public void OnDispose()
        {
            
        }
        
    }
}